package sgi.client;

import java.io.IOException;

import sgi.message.MessageDataLayer;
import sgi.message.MessageDateTime;
import sgi.message.MessageImagePack;

public class SGISubscribeController extends SGIController {
	public static SGISubscribeController subscribe;
	public static SGISubscribeUI subscribeUI;
	
	/** create controller */
	public static SGISubscribeController createSubscribe()
	{
		if( SGISubscribeController.subscribe == null )
		{
			SGISubscribeController.subscribe = new SGISubscribeController();
			subscribeUI.setSubscribeController(subscribe);
		}
		
		SGISubscribeController.subscribeUI.setVisible(true);
		return SGISubscribeController.subscribe;
	}
	
	/** constructor */
	public SGISubscribeController() {
		SGISubscribeController.subscribeUI = new SGISubscribeUI();
	}

	/** terminate UI */
	public static void terminate()
	{
		SGISubscribeController.subscribeUI.setVisible(false);
		SGISubscribeController.subscribe=null;
		SGISubscribeController.subscribeUI=null;
	}
	/** sending message of datalayer type of subscription with fields to insert to table */
	public void subscribeDateTime(String UserName,String location) throws IOException
	{
		MessageDateTime msg = new MessageDateTime(UserName,location);
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** sending message of datetime type of subscription with fields to insert to table */
	public void subscribeDataLayers(String UserName,int Commerce, int Infrastracture) throws IOException
	{
		MessageDataLayer msg = new MessageDataLayer(UserName,Commerce,Infrastracture);
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/** sending message of imagepack type of subscription with fields to insert to table */
	public void subscribeImagePack(String UserName) throws IOException
	{
		MessageImagePack msg = new MessageImagePack(UserName);
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	

	/** handle message that checks if customer already has been subscribed to datalayer */
	void handleMessageDataLayer(Object msg) {
		subscribeUI.notify(((MessageDataLayer)msg).getNotif());
		if(((MessageDataLayer)msg).isValid() == true)
		{
			SGISubscribeController.terminate();
			SGISubscribeController.createSubscribe();
		}
		else
		{
			SGISubscribeController.terminate();
			SGISubscribeController.createSubscribe();
		}

	}
	/** handle message that checks if customer already has been subscribed to datetime */
	void handleMessageDateTime(Object msg) {
		subscribeUI.notify(((MessageDateTime)msg).getNotif());
		if(((MessageDateTime)msg).isValid() == true)
		{
			SGISubscribeController.terminate();
			SGISubscribeController.createSubscribe();
		}
		else
		{
			SGISubscribeController.terminate();
			SGISubscribeController.createSubscribe();
		}

	}
	/** handle message that checks if customer already has been subscribed to imagepack */
	void handleMessageImagePack(Object msg) {
		subscribeUI.notify(((MessageImagePack)msg).getNotif());
		if(((MessageImagePack)msg).isValid() == true)
		{
			SGISubscribeController.terminate();
			SGISubscribeController.createSubscribe();
		}
		else
		{
			SGISubscribeController.terminate();
			SGISubscribeController.createSubscribe();
		}
	}
	
	void handleMessage(Object msg)
	{
		
	}
	/** getUI */
	public static SGISubscribeUI getUI()
	{
		return subscribeUI;
	}

}
